package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;
@Controller
@RequestMapping(value = "/details")	
public class DetailsClassController extends BaseController{

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;	
	private static Logger logger = LoggerFactory.getLogger(DetailsClassController.class);
	
	/*
	 * 课程详情查询接口
	 */	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/DetailsClass", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String detailsClass(HttpServletRequest request)
			throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse();
		
		try {	
			//课程ID
			String courseId  =request.getParameter("courseId");
			if(null != courseId && !courseId.isEmpty()){
				//查询课程详情
				List<Map<String, Object>> data = bizService.detailsClass(Integer.parseInt(courseId), Config.getImageAddress());	
				if(data != null && data.size() > 0){	
					response.setData(data);
					response.setErrorcode(0);	
				}else{	
					response.setErrorcode(1);
					response.setMessage("查询不到课程详细信息！");
				}	
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少参数！");	
			}
		} catch (Exception e) {
			logger.info("detailsClass:"+ e.toString());
			response.setReturnMsg(1,"课程详情查询失败！");
		}finally {
			return response.toJSON();
		}	
	}
	
	
	
	/*
	 * 取消预约页面---查询详细课程信息 根据课程编号查询
	 * 参数:课程编号(Id)
	 */
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/CancelAppointInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String CancelAppointInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse();
		
		try {
			//课程ID
			String CourseID = request.getParameter("CourseID");
			//学员ID
			String studentID=request.getParameter("studentID");
			if(!CourseID.isEmpty() && CourseID!=null){
				
				List<Map<String, Object>> data=new	ArrayList<Map<String, Object>>();
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("CourseID", CourseID);
				valMap.put("studentID", studentID);
				data=bizDao.CancelAppointInfo(valMap);
				
				if(data != null && data.size() > 0){
					
					response.setData(data);
					
				}else{
					
					response.setErrorcode(1);
					response.setData("无数据");
					
				}
				
			}else{
				response.setErrorcode(1);
				response.setMessage("课程ID不能为空！");
				
			}
			
		} catch (Exception e) {
			logger.info("CancelAppointInfo:"+ e.toString());
			response.setReturnMsg(1,"取消预约失败！");
		}finally {
			return response.toJSON();
		}
		
		
		
		
	}
	
}
